<?php
/**
 * Ora - 后台轮播管理模块
 * @author lan7
 * @copyright lan7.net
 * @version 2.0
 */

if(!defined('IN_ORA')) {
	exit('Access Denied');
}
class admin_jspic extends mod{
	function admin_jspic(){
		parent::__construct();
		session_start();
		$this->setTplDir(PUBLIC_PATH);
		$this->setMsgTpl("admin/msg.html");
		if(!isset($_SESSION[$GLOBALS['ora_cfg']['sess_prefix']."admin"])){
			$this->showMsg("请先登录!",parse_uri("common-login"),1);
		}
	}

	function index(){
		$do = $GLOBALS['_param']['do']?$GLOBALS['_param']['do']:'list';
		$xml = ROOT_PATH."public/xml/slides.xml";
		/**	列表 **/
		if($do=='list'){
			$str = file_get_contents($xml);
			if(sizeof($str)>0){
				$xobj = simplexml_load_string($str);
				$count = sizeof($xobj->album->slide);
				$xdata = array();
				for($j=0;$j<$count;$j++){
					$tmp = array();
					foreach($xobj->album->slide[$j]->attributes() as $a => $b) {
						$tmp[(string)$a] = (string)$b;
					}
					$xdata[] = $tmp;
				}
				$this->assign('xdata',$xdata);
			}
		}
		/**	删除 **/
		else if($do == 'del'){
			$id = $GLOBALS['_param']['id'];
			if(is_numeric($id)){
				$str = file_get_contents($xml);
				$xobj = simplexml_load_string($str);
				//若是本地图片,则删除
				foreach($xobj->album->slide[intval($id)]->attributes() as $a => $b) {
					if($a == 'd_URL' || $a == 'jpegURL'){
						if(strpos((string)$b,HTTP_HOST)>=0){
							io::remove(str_replace(HTTP_HOST."/",ROOT_PATH,(string)$b));
						}
					}
				}
				unset($xobj->album->slide[intval($id)]);
				@file_put_contents($xml,$xobj->asXML());

			}
			$this->showMsg("删除成功!",parse_uri('admin_jspic-index'),1);
		}
		/**	新增 **/
		else if($do == 'add'){
			if(isset($_POST['submit'])){
				import("lib.upload");
				$up = new Upload("jpg|gif|png");
				if($up->errInfo){
					$this->showMsg($up->errInfo);
				}
				$img = $up->files[0]["urlPath"];
				import("lib.image");
				//生成缩略图
				image::thumb(UPLOAD_PATH.$img,'',120,80);
				$str = file_get_contents($xml);
				$xobj = simplexml_load_string($str);
				$c = $xobj->album->addChild("slide");
				$c->addAttribute('title',$_POST['desc']?$_POST['desc']:'');
				$c->addAttribute('url',$_POST['href']?$_POST['href']:'');
				$c->addAttribute('d_URL',getRPath(HTTP_HOST."/public/xml/slider.xml",HTTP_HOST."/upload/".$img));
				$c->addAttribute('jpegURL',getRPath(HTTP_HOST."/public/xml/slider.xml",HTTP_HOST."/upload/".timg($img)));
				$c->addAttribute('phototime',3);
				$c->addAttribute('URLTarget',0);
				$c->addAttribute('transition',2);
				$c->addAttribute('panzoom',1);
				$c->addAttribute('width',924);
				$c->addAttribute('height',315);
				@file_put_contents($xml,$xobj->asXML());
				$this->showMsg("新增成功!",parse_uri('admin_jspic-index'),1);
			}
		}
		$this->assign('do',$do);
		$this->display('admin/jspic.html');
	}
}

?>